#include "s_sendneed.h"
#include "ui_s_sendneed.h"

#include <QDateTime>
#include <QMessageBox>
#include <QSqlQuery>

extern QString global3;
s_sendNeed::s_sendNeed(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::s_sendNeed)
{
    ui->setupUi(this);
}

s_sendNeed::~s_sendNeed()
{
    delete ui;
}

void s_sendNeed::on_submitBtn_clicked()
{
    QString str=QString("select * from student where sid='%1'").arg(global3);
    QSqlQuery query;
    query.exec(str);
    QString hid,sid,forwhat,roomid,bid,hname,sname;
    bool f1=false,f2=false;
    if(query.next()){
        f1=true;
        sid=query.value(0).toString();
        forwhat=ui->Edit->toPlainText();
        roomid=query.value(6).toString();
        sname=query.value(1).toString();
        bid=query.value(7).toString();
    }
    str=QString("select * from housemaster,hb where hid=Hou_Hid and bui_bid='%1'").arg(bid);
    query.exec(str);
    if(query.next()){
        f2=true;
        hid=query.value(0).toString();
        hname=query.value(1).toString();
    }
    if(f1 &&f2){
        str=QString("insert into water_a_repair(Hid,Stu_Sid,ForWhat,Sroomid,Hname,Sname)"
                    "values('%1','%2','%3','%4','%5','%6');")
                .arg(hid).arg(sid).arg(forwhat).arg(roomid).arg(hname).arg(sname);
        qDebug()<<str;
        if(query.exec(str)){
            QMessageBox::information(NULL,"tips","申请成功");
            this->close();
        }
        else QMessageBox::warning(NULL,"Error","申请失败,同时只允许进行一个调换申请");
    }
}

